package com.tmindtech.api.utils

import com.opencsv.CSVReader
import mu.KotlinLogging
import org.springframework.dao.DuplicateKeyException
import java.io.FileInputStream
import java.io.InputStream

/**
 * Created by RexQian on 2017/11/24.
 */
private val logger = KotlinLogging.logger {}

fun importCsv(path: String, processItem: (line: Array<String>) -> Boolean) {
    importCsv(FileInputStream(path), processItem)
}

fun importCsv(inputStream: InputStream, processItem: (line: Array<String>) -> Boolean) {
    val reader = CSVReader(inputStream.reader())
    var count = 0
    var importCount = 0
    for (line in reader.iterator()) {
        count++
        if (count <= 1) continue
        try {
            if (processItem(line)) {
                importCount++
            }
        } catch (ex: DuplicateKeyException) {
            //ignore
        } catch (ex: Exception) {
            logger.warn { "count: $count" }
            logger.warn { "line: ${line.contentToString()}" }
        }
        if (count % 100 == 0) {
            logger.info { "importing: $importCount/$count" }
        }
    }
    logger.info { "imported: $importCount/$count" }
}
